package top.archiesean.common.core.utils.resp;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import top.archiesean.common.core.domain.R;

import java.io.IOException;

/**
 * @author ArchieSean
 * @description 手动响应封装
 * @date 2024-01-08 10:04
 */
@Slf4j
public class ResponseUtils {

    /**
     * 响应
     *
     * @param response 响应对象
     * @param result   统一返回值结果
     */
    public static void out(HttpServletResponse response, R<?> result) {
        ObjectMapper mapper = new ObjectMapper();
        response.setStatus(HttpStatus.OK.value());
        response.setContentType("application/json;charset=UTF-8");
        try {
            mapper.writeValue(response.getWriter(), result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
